/*
 * Copyright (c) 2025 EasyAdmin
 * All rights reserved.
 *
 * This file is part of the [EasAdmin] project.
 * Unauthorized copying of this file, via any medium, is strictly prohibited.
 * Proprietary and confidential.
 *
 * Author: EasyAdmin ( 1073602@qq.com )
 * Website: https://www.xhxiao.com
 */
package com.easy.module.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.framework.common.utils.AssertUtil;
import com.easy.framework.common.utils.ConvertUtil;
import com.easy.framework.common.utils.Func;
import com.easy.module.system.entity.SysAppVersion;
import com.easy.module.system.mapper.SysAppVersionMapper;
import com.easy.module.system.query.SysAppVersionQuery;
import com.easy.module.system.service.ISysAppVersionService;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 应用版本 Service业务
 *
 * @author EasyAdmin ( 1073602@qq.com )
 * @date 2025-08-20
 */
@RequiredArgsConstructor
@Service
public class SysAppVersionServiceImpl extends ServiceImpl<SysAppVersionMapper, SysAppVersion> implements ISysAppVersionService {

    @Override
    public Boolean add(SysAppVersion dto) {
        AssertUtil.isFalse(exists(SysAppVersion::getVersion, dto.getVersion()), "版本号已经存在：" + dto.getVersion());
        String url = dto.getUrl();
        if (url.indexOf(".") > -1) {
            dto.setSuffix(url.substring(url.lastIndexOf(".")));
        }
        save(dto);
        return true;
    }

    @Override
    public Boolean update(SysAppVersion dto) {
        AssertUtil.isFalse(exists(SysAppVersion::getVersion, dto.getVersion(), SysAppVersion::getId, dto.getId()), "版本号已经存在：" + dto.getVersion());
        String url = dto.getUrl();
        if (url.indexOf(".") > -1) {
            dto.setSuffix(url.substring(url.lastIndexOf(".")));
        }
        updateById(dto);
        return true;
    }

    @Override
    public SysAppVersion getNewestVersion(SysAppVersionQuery query) {
        AssertUtil.isFalse(Func.isEmpty(query.getVersion()) || Func.isEmpty(query.getPackageName()), "非法参数！");
        SysAppVersion model = getOne(lambdaQueryWrapper()
                .eq(SysAppVersion::getPlatform, query.getPlatform())
                .eq(SysAppVersion::getStatus, 0)
                .gt(SysAppVersion::getVersion, query.getVersion())
                .eq(SysAppVersion::getPackageName, query.getPackageName()), false);
        return model;
    }

    @Override
    public SysAppVersion getVersionInfo(SysAppVersionQuery query) {
        AssertUtil.isFalse(Func.isEmpty(query.getVersion()) || Func.isEmpty(query.getPackageName()), "非法参数！");
        SysAppVersion model = getOne(lambdaQueryWrapper()
                .eq(SysAppVersion::getPlatform, query.getPlatform())
                .eq(SysAppVersion::getStatus, 0)
                .eq(SysAppVersion::getVersion, query.getVersion())
                .eq(SysAppVersion::getPackageName, query.getPackageName()), false);
        return model;
    }
}